<?php namespace testsacvoyage\storage;


require_once 'sacvoyage/Pathfinder.php';
use \sacvoyage\Pathfinder;

require_once 'sacvoyage/Object.php';
use \sacvoyage\Object;

require_once 'testsacvoyage/storage/AbstractTest.php';

class PathfinderTest extends AbstractTest {

	/**
	 * @dataProvider pairProvider
	 */
	public function test_get($json, $fields) {
		$finder=new Pathfinder($json);
		$this->assertEquals($finder->get(), $fields);
	}

	/**
	 * @dataProvider pairProvider
	 */
	public function test_set($json, $fields) {
		$finder=new Pathfinder($o=new Object());
		$finder->set($fields);
		$this->assertEquals($o, $json);
	}

	/**
	 * @dataProvider elementProvider
	 */
	public function test_both($json) {
		$finder=new Pathfinder($json);
		$fields=$finder->get();
		$finder=new Pathfinder($o=new Object());
		$finder->set($fields);
		$this->assertEquals($o, $json);
	}

	/**
	 * @dataProvider pairProvider
	 */
	public function test_iteration($json, $paths) {
		$finder=new Pathfinder($json);
		$this->assertEquals(iterator_to_array($finder), $paths);
	}



	public function pairProvider() {
	
		return array (
			
			array(
				new Object(array(
					"field1"=>"value1", 
					"field2"=>"value2"
				)),
				array(
					".field1"=>"value1", 
					".field2"=>"value2"
				)
			),
			
			array(
				new Object(array(
					"array"=>array(
						array(1,2,3), 
						array(4,5,6)
					),
					"object"=>new Object(array(
						"name"=> new Object(array(
							"first"=> "ivan", 
							"last"=> "ivanov", 
							"middle"=> array("I.", "S.")
						))
					))
				)),
				array(
					".array[0][0]"=>1,
					".array[0][1]"=>2,
					".array[0][2]"=>3,
					".array[1][0]"=>4,
					".array[1][1]"=>5,
					".array[1][2]"=>6,
					".object.name.first"    =>"ivan",
					".object.name.last"     =>"ivanov",
					".object.name.middle[0]"=>"I.",
					".object.name.middle[1]"=>"S."
				)
			),
			
			array(
				new Object(array("#some very strange keys' names"=>1)), 
				array("['#some very strange keys\' names']"=>1)				
			),

			array(
				new Object(), 
				array()				
			)
					
		);
	}

	public function elementProvider() {
	
		return array(
		
			array(new Object()),
		
			array(new Object(array('K'=>'V'))),

			array(new Object(array(
				"_id"=> "12fsi3rw3fwf",
				"_rev"=> "i3uhf78a3w8faw3fyh",
				"name"=> new Object(array(
					"first"=> "ivan",
					"last"=> "ivanov",
					"middles"=> array("I.", "S.", "K.")
				)),
				"address"=> "Str. App. 22",
				"friends"=> array("asuydfa8wef", "9w38fsl;df", "39fasldf")				
			))),
		
			array(new Object(array('text'=> 'Захватить мир!')))
	
		);

	}

}

?>
